package ru.madeira.jtetris;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GUIManager {


    public static final int TILE_SIZE = 32;
    public static final int BOTTOM_PANEL_SIZE = 100;
    public static final int RIGHT_PANEL_SIZE = TILE_SIZE * 5;

    private JFrame frame;
    private Game game;
    private JPanel panel;

    public GUIManager(Game game){
        this.game = game;
        frame = new JFrame();
        frame.setTitle("Java Tetris 2012");
        frame.setSize(RIGHT_PANEL_SIZE + game.getField().getWidth() * TILE_SIZE,
                BOTTOM_PANEL_SIZE + game.getField().getHeight() * TILE_SIZE);
        int sy = (Toolkit.getDefaultToolkit().getScreenSize().height - frame.getHeight()) / 2;
        int sx = (Toolkit.getDefaultToolkit().getScreenSize().width - frame.getWidth()) / 2;
        frame.setLocation(sx, sy);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        KeyListener k = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_RIGHT)
                   GUIManager.this.game.setCurrent_block_x(GUIManager.this.game.getCurrent_block_x() + 1);
                else
                if (key == KeyEvent.VK_LEFT)
                                   GUIManager.this.game.setCurrent_block_x(GUIManager.this.game.getCurrent_block_x() - 1);
                if (key == KeyEvent.VK_UP)
                   GUIManager.this.game.rotate_left();
                if (key == KeyEvent.VK_DOWN)
                   GUIManager.this.game.speedTimer();

            }

        };

        panel = new JPanel(){

            public void paintComponent(Graphics g){
                g.drawImage(ImageHolder.IMAGELIST[ImageHolder.IMG_BACKGROUND], 0, 0, this);
                Rectangle r = new Rectangle(0, GUIManager.this.game.getField().getHeight() * TILE_SIZE,
                        GUIManager.this.frame.getWidth(), BOTTOM_PANEL_SIZE);
                Graphics2D g2d = (Graphics2D)g;
                g2d.setPaint(Color.BLACK);
                g2d.fill(r);
                g2d.draw(r);
                r = new Rectangle(GUIManager.this.game.getField().getWidth() * TILE_SIZE, 0,
                        RIGHT_PANEL_SIZE, GUIManager.this.frame.getHeight());
                g2d.fill(r);
                g2d.draw(r);

                for (int i = 0; i<GUIManager.this.game.getField().getHeight(); i++)
                    for (int j = 0; j<GUIManager.this.game.getField().getWidth(); j++)
                        if (GUIManager.this.game.getField().getElement(i,j)!=0)
                            g.drawImage(ImageHolder.IMAGELIST[GUIManager.this.game.getField().getElement(i,j)], j * TILE_SIZE, i * TILE_SIZE, this);

                if (GUIManager.this.game.isHasMovingBlock()){
                    for (int i = 0; i<4; i++)
                        for (int j = 0; j<4; j++)
                            if (GUIManager.this.game.getCurrent_block().element[i][j]!=0)
                           g.drawImage(ImageHolder.IMAGELIST[GUIManager.this.game.getCurrent_block().element[i][j]], (j+GUIManager.this.game.getCurrent_block_x()) * TILE_SIZE, (i+GUIManager.this.game.getCurrent_block_y()) * TILE_SIZE, this);
                }
                int length = 0;
                int w =      GUIManager.this.game.getField().getWidth() * TILE_SIZE;
                Font f = new Font("Arial", Font.BOLD, 20);
                Font f2 = new Font("Times New Roman", Font.PLAIN, 18);
                Font f3 = new Font("Arial", Font.BOLD, 12);

                g2d.setFont(f);
                g2d.setPaint(Color.GREEN);
                g2d.drawString("Score", w + 50, length + 50);
                g2d.setFont(f2);
                g2d.drawString(format(Integer.toString(GUIManager.this.game.getScore())), w + 45, length + 80);

                g2d.setFont(f);
                g2d.setPaint(Color.YELLOW);
                g2d.drawString("HighScore", w + 30, length + 110);
                g2d.setFont(f2);
                g2d.drawString(format(Integer.toString(GUIManager.this.game.getHiscore())), w + 45, length + 140);


                g2d.setFont(f);
                g2d.setPaint(Color.BLUE);
                g2d.drawString("Level", w + 50, length + 170);
                g2d.setFont(f2);
                g2d.drawString(Integer.toString(GUIManager.this.game.getLevel()), w + 70, length + 200);


                g2d.setFont(f);
                g2d.setPaint(Color.GRAY);
                g2d.drawString("Lines", w + 50, length + 230);
                g2d.setFont(f2);
                g2d.drawString(Integer.toString(GUIManager.this.game.getLines_removed()), w + 70, length + 260);


                g2d.setFont(f);
                g2d.setPaint(Color.WHITE);
                g2d.drawString("NEXT", w + 50, length + 290);

                if (GUIManager.this.game.getNext_block()!=-1){
                    for (int i = 0; i<4; i++)
                        for (int j=0; j<4; j++)
                            if (TemplateSet.TEMPLATESET[GUIManager.this.game.getNext_block()].element[i][j]!=0)
                            g2d.drawImage(ImageHolder.IMAGELIST[TemplateSet.TEMPLATESET[GUIManager.this.game.getNext_block()].element[i][j]],  j * TILE_SIZE + w + 30, i * TILE_SIZE + 300,this);
                }

                if (GUIManager.this.game.getRandomphrase()!=null){
                    g2d.setFont(f3);
                    g2d.setPaint(Color.RED);
                    g2d.drawString(GUIManager.this.game.getRandomphrase(), 50, GUIManager.this.game.getField().getHeight() * TILE_SIZE + 20);
                }

                g2d.setFont(f);
                g2d.setPaint(Color.WHITE);
                g2d.drawString("Remaining", w + 40, length + 440);
                g2d.setFont(f2);
               g2d.drawString(Integer.toString(GUIManager.this.game.getNext_lines()), w + 70, length + 480);






            }

        };
        panel.setFocusable(true);
        panel.addKeyListener(k);
        frame.add(panel);
        frame.setVisible(true);


    }

     public void updateScreen(){
         panel.repaint();
     }

     private String format(String s){
         String n = new String(s);
         while (n.length()<7)
             n = "0" + n;
         return n;
     }

}
